package com.online.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.online.pojo.UserRole;
import com.online.user.service.UserRoleService;
import com.online.user.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author 瑾
* @description 针对表【on_user_role】的数据库操作Service实现
* @createDate 2025-01-26 18:53:51
*/
@Service
public class UserRoleServiceImpl implements UserRoleService{
    @Autowired
    private UserRoleMapper iUserRoleMapper;

    @Override
    public List<UserRole> selectAll() {
        LambdaQueryWrapper <UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getIsDelete,0);
        return iUserRoleMapper.selectList(lqw);
    }


    @Override
    public List<UserRole> selectList(UserRole iUserRole) {
        LambdaQueryWrapper <UserRole> lqw = this.lqw(iUserRole);
        return iUserRoleMapper.selectList(lqw);
    }

    @Override
    public Integer insert(UserRole iUserRole) {
        return iUserRoleMapper.insert(iUserRole);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                UserRole iUserRole = new UserRole();
                iUserRole.setId(Integer.valueOf(id));
                iUserRole.setIsDelete(1);
                iUserRoleMapper.updateById(iUserRole);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(UserRole iUserRole) {
        return iUserRoleMapper.updateById(iUserRole);
    }

    @Override
    public Page<UserRole> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<UserRole>)this.selectAll();
    }

    @Override
    public Page<UserRole> pageList(UserRole iUserRole, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<UserRole>)this.selectList(iUserRole);
    }

    public LambdaQueryWrapper lqw(UserRole iUserRole){
        LambdaQueryWrapper <UserRole> lqw = new LambdaQueryWrapper<>();
        if (iUserRole.getId() != null){
            lqw.eq(UserRole::getId,iUserRole.getId());
        }
        if (iUserRole.getUserId() != null){
            lqw.eq(UserRole::getUserId,iUserRole.getUserId());
        }
        if (iUserRole.getRoleId() != null){
            lqw.eq(UserRole::getRoleId,iUserRole.getRoleId());
        }
        return lqw;
    }
}




